<?php
/**
 * AdMinHTMLTabs is a simple class that prints the HTML for tabs
 *
 * Here is an example of usage
 * <code>
 * require_once(DIR_CLASSES ."/User.php");
 *
 * // Tabs example:
 * <?
 * $tabs = new AdMinHTMLTabs(MODULE_ID, SUBMODULE_ID, $allowNexSubModule); // The object takes the sub modules from the $GLOBALS['CONF'] array
 * $tabs->printHTML();                                 // TThat's it.
 * ...
 * </code>
 *
 *  2 USAGES:
 *  - Parallel: All sub modules can be accessed at the same time.
 *  - Sequence: Module's sub modules are accessed 1 at a time, in the sequence defined in the config.php file.
 *              To access modules in sequence, module should have the variable "subModulesAccess" defined as "sequence".
 *              The parameter "$allowNexSubModule" enables the next sub module to be accessed. Otherwise, the access can be done manually outside AdMinHTMLTabs.
 *
 *
 *
 * @package AdMin Framework
 */
class AdMinHTMLTabs extends AdMinHTML
{
    public  $moduleID = "";
    public  $moduleConfig = array();
    public  $currentSubModuleID = "";
    public  $htmlClasses = array("icons" => "Icons",
                                 "separator" => "Separator");
    public  $icoWidth = 32;
    public  $icoHeight = 32;
    public  $excludeCurrentModule = true;
    public  $showSubModules = true;
    public  $moduleVars = "";
    public  $subModulesAccess = "parallel"; // TYPES parallel, sequence.
    public  $allowNextSubModule = false; // this variable is changed outside. Useful for sequenced access.
    private $_arActions = array();
    private $_arSubModules = array();


    public function __construct($moduleID = '', $currSubModuleID = '', $allowNextSubModule = false)
    {
        parent::__construct();

        $this->allowNextSubModule = $allowNextSubModule;
        $this->moduleID = $moduleID;
        $this->currentSubModuleID = $currSubModuleID;
        if (is_array($GLOBALS["CONF"]["admin"]["modules"][$this->moduleID]))
        {
            $this->moduleConfig = $GLOBALS["CONF"]["admin"]["modules"][$this->moduleID];
            $this->subModulesAccess = $GLOBALS["CONF"]["admin"]["modules"][$this->moduleID]["subModulesAccess"] ? $GLOBALS["CONF"]["admin"]["modules"][$this->moduleID]["subModulesAccess"] : "parallel";
            if($this->showSubModules)
            {
                $this->setSubModules($this->moduleConfig['subModules']);
            }
        }
        if($currSubModuleID && is_array($GLOBALS["CONF"]["admin"]["modules"][$this->moduleID]["subModules"][$currSubModuleID]['subModules']))
        {
            $this->setActions($GLOBALS["CONF"]["admin"]["modules"][$this->moduleID]["subModules"][$currSubModuleID]['subModules']);
        }

    }

    public function setActions($actions)
    {
        if (!is_array($actions))
        {
            $this->_error("\$actions in AdMinHTMLTabs->setActions(\$actions) must be an array of actions.");
            return 0;
        }
        $this->_arActions = $actions;
    }

    public function setSubModules($subModules)
    {
        if (!is_array($subModules))
        {
            $this->_error("\$subModules in AdMinHTMLTabs->setSubModules(\$subModules) must be an array of sub modules.");
            return 0;
        }
        $this->_arSubModules = $subModules;
        if($this->subModulesAccess == "sequence")
        {
            $this->setSequencedSubModules();
        }

    }

    protected function setSequencedSubModules()
    {
        $prevSubModuleID = NULL;
        $available = true;
        foreach($this->_arSubModules as $subModuleID => $subModule)
        {
            $this->_arSubModules[$subModuleID]["available"] = $available;
            // THIS MODULE IS THE NEXT STEP, OR THE FIRST.

            if($this->currentSubModuleID == $prevSubModuleID)
            {
                $this->_arSubModules[$subModuleID]["available"] = $this->allowNextSubModule ? true : $available;
            }
            if($this->currentSubModuleID == $subModuleID || !$this->currentSubModuleID)
            {
                $available = false;
            }
            $prevSubModuleID = $subModuleID;
        }
    }

    private function _bluidIcon($id, $url, $name, $icoUrl)
    {
        if($url)
        {
            return "\n\t<div ".($this->currentSubModuleID == $id ? "class='current'" : "")."><a href=\"". $url .'"><img width="'. $this->icoWidth .'" height="'. $this->icoHeight .'" src="'. $icoUrl .'"/><br/>'. $name .'</a></div>';
        }
        else
        {
            return '<div class="unavailable"><img width="'. $this->icoWidth .'" height="'. $this->icoHeight .'" src="'. $icoUrl .'"/><br/>'. $name .'</div>';
        }
    }

    public function build()
    {
        $this->_htmlBuffer = "\n<div class=\"". $this->htmlClasses['icons'] ."\">";
        /*
        * Actions are built
        */
        foreach($this->_arActions AS $actionID => $action)
        {
            if ($this->moduleVars) $action['url'] .= (strstr("?", $action['url'])? "&" : "?") . $this->moduleVars;
            $this->_htmlBuffer .= $this->_bluidIcon($actionID, $action['url'], $action['name'], $action['ico']);
        }

        $this->_htmlBuffer .= count($this->_arActions)>0&&count($this->_arSubModules)>0? "\n\t<div class=\"". $this->htmlClasses['separator'] ."\"/></div>" : "";

        if($this->showSubModules)
        {
            /*
            * Sub Modules are built
            */
            foreach($this->_arSubModules AS $subModuleID => $subModule)
            {
                if ($this->excludeCurrentModule && $subModuleID==$this->currentSubModuleID && $this->subModulesAccess == "parallel") continue;
                if($this->subModulesAccess == "sequence" && !$subModule["available"])
                {
                    $subModule['url'] = '';
                }
                else
                {
                    $subModule['url'] = preg_match('/http:\/\//', $subModule['url'])? $subModule['url'] : $this->moduleConfig['url'] ."/". $subModule['url'];
                    if ($this->moduleVars) $subModule['url'] .= (strstr("?", $subModule['url'])? "&" : "?") . $this->moduleVars;
                }
                $this->_htmlBuffer .= $this->_bluidIcon($subModuleID, $subModule['url'], $subModule['name'], $subModule['ico']);
            }
        }

        $this->_htmlBuffer .= "\n</div>\n";
    }

    public function buildExtjsTabs()
    {
        $this->_htmlBuffer = "";
        if($this->showSubModules)
        {
            $i=0;
            foreach($this->_arSubModules AS $subModuleID => $subModule)
            {
                if ($this->excludeCurrentModule && $subModuleID==$this->currentSubModuleID && $this->subModulesAccess == "parallel") continue;
                if($this->subModulesAccess == "sequence" && !$subModule["available"])
                {
                    $subModule['url'] = '';
                }
                else
                {
                    $subModule['url'] = preg_match('/http:\/\//', $subModule['url'])? $subModule['url'] : $this->moduleConfig['url'] ."/". $subModule['url'];
                    if ($this->moduleVars) $subModule['url'] .= (strstr("?", $subModule['url'])? "&" : "?") . $this->moduleVars;
                }
                $this->_htmlBuffer .= "{ title: '". $subModule['name'] ."',\n";
                $this->_htmlBuffer .= "  html: '<iframe src=\"". $subModule['url'] ."\" style=\"width: 100%; height:100%; border: none\" frameborder=\"0\"></iframe>' }";
                $i++;
                $this->_htmlBuffer .= $i<count($this->_arSubModules)? "," : "";
            }
        }
    }
    
    public function numOfTabs()
    {
        return count($this->_arSubModules);
    }

    public function printExtjsHTML()
    {
        $this->buildExtjsTabs();
        echo $this->getHTML();
    }

}

?>

